package com.xgr.wonderful.adapter;

import java.util.Date;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.easemob.chat.EMConversation;
import com.easemob.chat.EMMessage;
import com.easemob.chat.TextMessageBody;
import com.easemob.util.DateUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xgr.wonderful.MyApplication;
import com.xgr.wonderful.R;
import com.xgr.wonderful.entity.SuperConversation;
import com.xgr.wonderful.view.CircleImageView;

public class ConversationAdapter extends BaseContentAdapter<SuperConversation> {

	public ConversationAdapter(Context context, List<SuperConversation> list) {
		super(context, list);
		
	}

	@Override
	public View getConvertView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder mViewHolder = null ;
		if (convertView == null) {
			mViewHolder = new ViewHolder();
			convertView = mInflater.inflate(
					R.layout.item_conversation, null);
			mViewHolder.mAvatar = (CircleImageView) convertView
					.findViewById(R.id.conversation_item_avatar);
			mViewHolder.mMsg = (TextView) convertView
					.findViewById(R.id.conversation_item_msg_content);
			mViewHolder.mMsgState = (ImageView) convertView
					.findViewById(R.id.conversation_item_msg_state);
			mViewHolder.mTime = (TextView) convertView
					.findViewById(R.id.conversation_item_time);
			mViewHolder.mUnread = (TextView) convertView
					.findViewById(R.id.conversation_item_unread_number);
			mViewHolder.mUsername = (TextView) convertView
					.findViewById(R.id.conversation_item_username);
			mViewHolder.mListItemLayout = (LinearLayout) convertView
					.findViewById(R.id.conversation_item_list_layout);
			convertView.setTag(mViewHolder);
		} else {
			mViewHolder = (ViewHolder) convertView.getTag();
		}
		//conversation背景设置，可以不用的
//		if (position % 2 == 0) {
//			mViewHolder.mListItemLayout
//					.setBackgroundResource(R.drawable.mm_listitem);
//		} else {
//			mViewHolder.mListItemLayout
//					.setBackgroundResource(R.drawable.mm_listitem_grey);
//		}
//		mViewHolder.mListItemLayout
//		.setBackgroundResource(R.drawable.mm_listitem);
		SuperConversation conversation = getItem(position);
		if (conversation.getRemoteUserPhoto()!= null && 
				conversation.getRemoteUserPhoto().getSmall()!= null){
			ImageLoader.getInstance().displayImage(
					conversation.getRemoteUserPhoto().getSmall(),
					mViewHolder.mAvatar,
					MyApplication.getInstance().getOptions(
							R.drawable.user_icon_default));
		}
		
		mViewHolder.mUsername.setText(conversation.getRemoteUsername());
		EMConversation emConversation = conversation.getEmConversation();
		int unread = emConversation.getUnreadMsgCount();
		if (unread > 0) {
			mViewHolder.mUnread.setText(String.valueOf(unread));
			mViewHolder.mUnread.setVisibility(View.VISIBLE);
		} else {
			mViewHolder.mUnread.setVisibility(View.INVISIBLE);
		}
		if (emConversation.getMsgCount() != 0) {
			// 把最后一条消息的内容作为item的message内容
			EMMessage lastMessage = emConversation.getLastMessage();
			mViewHolder.mMsg.setText(getMessageContent(lastMessage));
			mViewHolder.mTime.setText(DateUtils.getTimestampString(new Date(
					lastMessage.getMsgTime())));
			if (lastMessage.direct == EMMessage.Direct.SEND
					&& lastMessage.status == EMMessage.Status.FAIL) {
				// 是从我这边 发送的消息，且发送失败
				mViewHolder.mMsgState.setVisibility(View.VISIBLE);
			} else {
				mViewHolder.mMsgState.setVisibility(View.INVISIBLE);
			}
		} else {
			mViewHolder.mMsg.setText("");
			mViewHolder.mTime.setText("");
			mViewHolder.mMsgState.setVisibility(View.INVISIBLE);
		}
		return convertView;
	}

	/**
	 * 取出消息内容
	 * @param message
	 * @return
	 */
	private String getMessageContent(EMMessage message) {
		String content = "";
		switch (message.getType()) {
		case TXT:
			TextMessageBody txtBody = (TextMessageBody) message.getBody();
			content = txtBody.getMessage();
			break;
		default:
			System.err.println("error, unknow type");
			return "";
		}
		return content;
	}

	private class ViewHolder {
		/** 和谁的聊天记录 */
		TextView mUsername;
		/** unread msg **/
		TextView mUnread;
		/** 最后一条消息的时间 **/
		TextView mTime;
		/** 最后一条消息的内容 **/
		TextView mMsg;
		/** 最后一条消息的发送状态 ， 可以先不考虑 **/
		ImageView mMsgState;
		/** 用户头像 */
		CircleImageView mAvatar;
		/** 整个list中每一行总布局 */
		LinearLayout mListItemLayout;
	}

}
